package com.wondersgroup.fjzlpt.org.dao.impl.jgqy.qysq;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqDAO;
@Repository
public class QysqDAOImpl extends BaseJTDAOImpl implements QysqDAO{
	@Override
	@Autowired
	@Qualifier("TB_JGQY_YLT")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}

	/**
	 * 分页查询
	 */
	public JSONObject queryPage(Map<String, String> map) {
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql");
		List<String> list = (List)sqlObj.get("sqlParams");
		
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sql, "order by a.YLTID DESC,a.QYSJ DESC", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	/**
	 * 根据条件查询所有（导出）
	 * @param map
	 * @return
	 */
	public JSONArray queryAll(Map<String, String> map){
		JSONObject sqlObj = this.getPageSql(map);
		String sql = sqlObj.getString("sql") + " order by a.YLTID DESC,a.QYSJ DESC";
		List<String> list = (List)sqlObj.get("sqlParams");
		
		SQLBean bean = new SQLBean(sql, list.toArray());
		return super.queryList(bean);
	}
	
	/**
	 * 分页查询基本sql
	 * @param map
	 * @return
	 */
	private JSONObject getPageSql(Map<String, String> map){
		JSONObject result = new JSONObject();
		
		StringBuffer sbuf = new StringBuffer("select a.YLJGID,a.QYZT,a.JGLX,a.QYYXQ,a.QYSJ,a.DQSJ,a.GLID,b.*, ");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = a.YLJGID) YLJGMC,");
		sbuf.append("c.YLJGMC ZDYLJGMC,");
		sbuf.append("(select XMMC from TB_ZY_YXQ where XMDM = a.QYYXQ) QYYXQMC,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = b.YLTLX) YLTLXMC \n");
		sbuf.append(" from TB_JGQY_YLT_YLJG a, TB_JGQY_YLT b,TB_ZD_YLJGXX c ");
		sbuf.append(" where a.YLTID = b.YLTID and b.ZDYLJGID = c.PTJGID and a.YLJGID <> b.ZDYLJGID  and a.JLZT = '1' ");
		List list = new ArrayList();
		String value = map.get("JGLX");
		if(StringUtils.isEmpty(value))
		{
			sbuf.append("and a.JGLX = '1' ");
			value = map.get("ZDYLJGID");
			if(!StringUtils.isEmpty(value))
			{
				sbuf.append("and b.ZDYLJGID = ? ");
				list.add(value);
			}
		}else{
			sbuf.append("and a.JGLX = ? ");
			list.add(value);
			value = map.get("ZDYLJGID");
			if(!StringUtils.isEmpty(value))
			{
				sbuf.append("and a.YLJGID = ? ");
				list.add(value);
			}
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.YLJGID = ? ");
			list.add(value);
		}
		value = map.get("YLTLX");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and b.YLTLX = ? ");
			list.add(value);
		}
		value = map.get("CJSJ");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and b.CJSJ = ? ");
			list.add(value);
		}
		/*value = map.get("JSSJ");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.QYSJ < ? ");
			list.add(value);
		}*/
		value = map.get("YLTID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.YLTID = ? ");
			list.add(value);
		}
		value = map.get("YLTMC");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and a.YLTMC like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("ZDYLJGMC");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append("and c.YLJGMC like ? ");
			list.add("%"+value+"%");
		}
		
		result.put("sql", sbuf.toString());
		result.put("sqlParams", list);
		
		return result;
	}

	/**
	 * 查询签约机构
	 * @param map
	 * @return
	 */
	public JSONArray queryQyJg(Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer("select a.JGLX,a.YLTID,a.QYZT,a.GLID,a.QYYXQ,a.QYSJ,a.DQSJ,b.*, ");
		sbuf.append("(select XMMC from TB_ZY_JGQY_QYZT where XMDM = a.QYZT) QYZTMC \n ");
		sbuf.append(" from TB_JGQY_YLT_YLJG a, VIEW_YLJGXX b ");
		sbuf.append(" where a.YLJGID = b.PTJGID and a.JGLX in (1,3) and a.QYZT in (01,02,03,07) and a.JLZT = '1'  and  a.YLTID = ? ");//可以查询不同医疗机构数据
		SQLBean sb = new SQLBean(sbuf.toString(),new String[]{map.get("YLTID")});
		return super.queryList(sb);
	}

	/**
	 * 查询医联体信息
	 * @param map
	 * @return
	 */
	public JSONObject queryYltxx(Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer("select a.*,");
		sbuf.append("b.YLJGMC  ZDYLJGMC,c.QYYXQ,c.QYSJ,c.DQSJ,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = a.YLTLX) YLTLXMC,");
		sbuf.append("(select XMMC from TB_ZY_YXQ where XMDM = c.QYYXQ) QYYXQMC \n");
		sbuf.append(" from "+this.tb.getTableName()+" a ");
		sbuf.append(" left join TB_ZD_YLJGXX b on  a.ZDYLJGID = b.PTJGID ");
		sbuf.append(" left join TB_JGQY_YLT_YLJG c on a.YLTID=c.YLTID  ");
		sbuf.append("  where c.JGLX='3' and c.JLZT = '1'  ");
		List list = new ArrayList();
		String value = map.get("YLTID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTID = ? ");
			list.add(value);
		}
		value = map.get("YLTMC");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTMC like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("YLTLX");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTLX = ? ");
			list.add(value);
		}
		value = map.get("ZDYLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.ZDYLJGID = ? ");
			list.add(value);
		}
		value = map.get("ZDYLJGMC");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and b.YLJGMC like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("YLTZT");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTZT = ? ");
			list.add(value);
		}
		value = map.get("YLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTID not in (select distinct YLTID from TB_JGQY_YLT_YLJG where YLJGID = ? and QYZT in (01,02,03) and JLZT = '1')");
			list.add(value);
		}
		value = map.get("TGYLJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbuf.append(" and a.YLTID in (select distinct YLTID from TB_JGQY_YLT_YLJG where YLJGID = ? and QYZT in (03,05) and JLZT = '1')");
			list.add(value);
		}
		//sbuf.append(" where a.YLTID = ? ");
		/*SQLBean sb = new SQLBean(sbuf.toString(),new String[]{map.get("YLTID")});
		return super.queryOne(sb);*/
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sbuf.toString(), " order by a.YLTID,c.QYSJ ", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}

	/**
	 * 根据医联体ID查询签约申请
	 * @param yltidAry
	 * @return
	 */
	/*public JSONArray queryList(String[] yltidAry) {
		StringBuffer sbuf = new StringBuffer("select a.YLJGID,a.QYZT,a.JGLX,a.QYYXQ,b.*, ");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = a.YLJGID) YLJGMC,");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = b.ZDYLJGID) ZDYLJGMC,");
		sbuf.append("(select XMMC from TB_ZY_YXQ where XMDM = a.QYYXQ) QYYXQMC,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = b.YLTLX) YLTLXMC \n");
		sbuf.append(" from TB_JGQY_YLT_YLJG a, TB_JGQY_YLT b ");
		sbuf.append(" where a.YLTID = b.YLTID and a.JLZT = '1' and a.YLJGID <> b.ZDYLJGID  and a.YLTID in (");
		String params = "";
		for (String string : yltidAry) {
			params += "?,";
		}
		params = params.substring(0, params.lastIndexOf(","));
		sbuf.append(params);
		sbuf.append(") \n");
		SQLBean sb = new SQLBean(sbuf.toString(), yltidAry);
		return super.queryList(sb);
	}*/
	
	
	/**
	 * 根据医联体ID查询医联体
	 * @param yltidAry
	 * @return
	 */
	/*public JSONArray queryYltList(String[] yltidAry) {
		StringBuffer sbuf = new StringBuffer("select a.*,b.QYSJ,b.DQSJ ");
		sbuf.append("(select YLJGMC from TB_ZD_YLJGXX where PTJGID = a.ZDYLJGID) ZDYLJGMC,");
		sbuf.append("(select XMMC from TB_ZY_YXQ where XMDM = b.QYYXQ) QYYXQMC,");
		sbuf.append("(select XMMC from TB_ZY_YLTLX where XMDM = a.YLTLX) YLTLXMC \n");
		sbuf.append(" from "+this.tb.getTableName()+" a,TB_JGQY_YLT_YLJG b where a.YLTID=b.YLTID and b.JGLX='3' and b.JLZT = '1'  ");
		sbuf.append(" where a.YLTID in (");
		String params = "";
		for (String string : yltidAry) {
			params += "?,";
		}
		params = params.substring(0, params.lastIndexOf(","));
		sbuf.append(params);
		sbuf.append(") \n");
		SQLBean sb = new SQLBean();
		sb.setSql(sbuf.toString());
		sb.setParamAry(yltidAry);
		return super.queryList(sb);
	}*/

	/**
	 * 判断医联体是否已经存在
	 * @param map
	 * @return
	 */
	public boolean isExit(Map<String, String> map) {
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("select count(YLTID) from "+this.tb.getTableName()+" where YLTMC = ?");
		SQLBean  sb = new SQLBean(sbuf.toString(),new String[]{map.get("YLTMC")});
		return super.isExist(sb);
	}

}
